package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.Currency;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class CurrencyDAO extends AbstractDao<Currency>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_currency";
	public static final String SELECT_SQL = "select * from oce_currency where currencyId = ?";
	
	public Currency create(Currency obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public Currency update(Currency obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public Currency find(long id){
		List<Currency> currencies = new ArrayList<Currency>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			currencies = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return currencies.size() == 0 ? null : currencies.get(0);
	}
	
	public List<Currency> findAll(){
		List<Currency> currencies = new ArrayList<Currency>();
		try{
			currencies = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			currencies = null;
		}
		return currencies;
	}
	
	public void delete(Currency obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<Currency> convert(ResultSet rs) throws SQLException {
		List<Currency> currencies = new ArrayList<Currency>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String name = rs.getString(2);
	        String sign = rs.getString(3);
	        currencies.add(new Currency(id, name, sign));
	    }
		rs.close();
		return currencies;
	}
}
